package com.eostoken.eos_wallet.mvp.mode;

import android.app.Activity;
import android.content.Context;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.eostoken.eos_wallet.bean.AccountInfo;
import com.eostoken.eos_wallet.bean.AccountNames;
import com.eostoken.eos_wallet.bean.EOSPrice;
import com.eostoken.eos_wallet.eosnet.common.EOSNetWorkManager;
import com.eostoken.eos_wallet.eosnet.common.Params;
import com.eostoken.eos_wallet.mvp.callback.CommonCallback;
import com.eostoken.eos_wallet.net.common.NetWorkManager;
import com.eostoken.eos_wallet.net.common.RxSubscriber;
import com.eostoken.eos_wallet.utils.Constants;
import com.fredy.mvp.Model;
import com.google.gson.JsonObject;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import one.block.eosiojava.models.rpcProvider.response.GetInfoResponse;

import static com.eostoken.eos_wallet.utils.Constants.ACCOUNTS;
import static com.eostoken.eos_wallet.utils.Constants.ACCOUNT_INFO;
import static com.eostoken.eos_wallet.utils.Constants.BALANCE;
import static com.eostoken.eos_wallet.utils.Constants.GETINFO;
import static com.eostoken.eos_wallet.utils.Constants.PRICE;

public class HomeModel implements Model {

    public void getChainInfo(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback, int type) {
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getInfo())
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<GetInfoResponse>(context, showLoading) {

                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getCurrencyBalance onFail: " + failMsg);
                        callback.onFailure(failMsg, GETINFO);
                    }

                    @Override
                    public void onSuccess(GetInfoResponse getInfoResponse) {
                        if (type == BALANCE) {
                            getCurrencyBalance(context, map, showLoading, callback);
                        }
                        String json = GsonUtils.toJson(getInfoResponse);
                        LogUtils.dTag(Constants.TAG_MGP, "getCurrencyBalance onSuccess: " + json);
                        callback.onSuccess(getInfoResponse, "", GETINFO);
                    }
                });
    }


    public void getCurrencyBalance(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback) {
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getCurrencyBalance(Params.getRequestBody(map)))
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<String>(context, showLoading) {

                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getCurrencyBalance onFail: " + failMsg);
                        callback.onFailure(failMsg, BALANCE);
                    }

                    @Override
                    public void onSuccess(String s) {
                        LogUtils.dTag(Constants.TAG_MGP, "getCurrencyBalance onSuccess: " + s);
                        callback.onSuccess(s, "", BALANCE);
                    }
                });
    }

    public void getKeyAccounts(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback) {
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getKeyAccounts(Params.getRequestBody(map)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<String>(context, showLoading) {

                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getKeyAccounts onFail: " + failMsg);
                        callback.onFailure(failMsg, ACCOUNTS);
                    }

                    @Override
                    public void onSuccess(String s) {
                        AccountNames accountNames = GsonUtils.fromJson(s, AccountNames.class);
                        LogUtils.dTag(Constants.TAG_MGP, "getKeyAccounts onSuccess: " + s);
                        callback.onSuccess(accountNames, "", ACCOUNTS);
                    }
                });
    }

    public void getDIGICCYPrice(Activity context, String pair, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getDIGICCYPrice(pair)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<EOSPrice>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        callback.onFailure(failMsg, PRICE);
                    }

                    @Override
                    public void onSuccess(EOSPrice eosPrice) {
                        callback.onSuccess(eosPrice, "", PRICE);
                    }
                });
    }

    public void getAccount(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback) {
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getAccount(Params.getRequestBody(map)))
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<String>(context, showLoading) {

                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getAccount onFail: " + failMsg);
                        callback.onFailure(failMsg, ACCOUNT_INFO);
                    }

                    @Override
                    public void onSuccess(String s) {
                        LogUtils.dTag(Constants.TAG_MGP, "getAccount onSuccess: " + s);
                        AccountInfo accountInfo = GsonUtils.fromJson(s, AccountInfo.class);
                        callback.onSuccess(accountInfo, "", ACCOUNT_INFO);
                    }
                });
    }

    public void getBindWalletAddress(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getBindWalletAddress(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }
}